package com.bytedance.retrofit2;

import com.bytedance.retrofit2.client.Header;
import com.bytedance.retrofit2.mime.FormUrlEncodedTypedOutput;
import com.bytedance.retrofit2.mime.MultipartTypedOutput;
import com.bytedance.retrofit2.mime.TypedOutput;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public final class RequestBuilder {
    final HttpUrl httpUrl;
    final RequestInterceptor requestInterceptor;
    final MultipartTypedOutput multipartBody;
    private final FormUrlEncodedTypedOutput formBody;
    public String url;
    public List<Header> headers;
    public boolean n;
    public Object o;
    String httpMethod;
    String relativeUrl;
    StringBuilder e;
    String contentType;
    TypedOutput body;
    PriorityLevel priorityLevel;
    boolean isStreaming;
    int m;

    RequestBuilder(String httpMethod, HttpUrl baseUrl, String relativeUrl, List<Header> headers,
                   String contentType, RequestInterceptor requestInterceptor,
                   PriorityLevel priorityLevel, boolean isStreaming, int arg11, boolean arg12, Object arg13,
                   boolean isFormEncoded, boolean isMultipart) {
        this.httpMethod = httpMethod;
        this.httpUrl = baseUrl;
        this.relativeUrl = relativeUrl;
        this.contentType = contentType;
        this.requestInterceptor = requestInterceptor;
        this.priorityLevel = priorityLevel;
        this.isStreaming = isStreaming;
        this.m = arg11;
        this.n = arg12;
        this.o = arg13;
        this.headers = headers;
        if (isFormEncoded) {
            this.formBody = new FormUrlEncodedTypedOutput();
            this.multipartBody = null;
            this.body = this.formBody;
        } else if (isMultipart) {
            this.formBody = null;
            this.multipartBody = new MultipartTypedOutput();
            this.body = this.multipartBody;
        } else {
            this.formBody = null;
            this.multipartBody = null;
        }
    }

    final void addHeader(String name, String value) {
        List v0_1 = null;
        if (name == null) {
            throw new IllegalArgumentException("Header name must not be null.");
        }
        if ("Content-Type".equalsIgnoreCase(name)) {
            this.contentType = value;
        } else {
            v0_1 = this.headers;
            if (v0_1 == null) {
                v0_1 = new ArrayList(2);
                this.headers = v0_1;
            }
            v0_1.add(new Header(name, value));
        }
    }

    final void addQueryParam(String name, String value, boolean urlEncodeValue) {
        char v0_2;
        StringBuilder v1;
        StringBuilder v0_1;
        if (name == null) {
            throw new IllegalArgumentException("Query param name must not be null.");
        }

        try {
            v0_1 = this.e;
            if (v0_1 == null) {
                v0_1 = new StringBuilder();
                this.e = v0_1;
                v1 = v0_1;
            } else {
                v1 = v0_1;
            }

            if (v1.length() > 0) {
                v0_2 = '&';
            } else {
                v0_2 = '?';
            }

            v1.append(v0_2);
            if (urlEncodeValue) {
                name = URLEncoder.encode(name, "UTF-8");
            }

            if (urlEncodeValue) {
                value = URLEncoder.encode(value, "UTF-8");
            }

            v1.append(name).append('=').append(value);
        } catch (UnsupportedEncodingException v0) {
            throw new RuntimeException("Unable to convert query parameter \"" + name + "\" value to UTF-8: " + value, ((Throwable) v0));
        }
    }

    final void b(String arg3, String arg4, boolean arg5) {
        this.formBody.addField(arg3, arg5, arg4.toString(), arg5);
    }

    static final class MimeOverridingTypedOutput implements TypedOutput {
        private final TypedOutput delegate;
        private final String mimeType;

        MimeOverridingTypedOutput(TypedOutput delegate, String mimeType) {
            this.delegate = delegate;
            this.mimeType = mimeType;
        }

        public final String mimeType() {
            return this.mimeType;
        }

        public final void writeTo(OutputStream out) throws IOException {
            this.delegate.writeTo(out);
        }

        public final long length() {
            return this.delegate.length();
        }

        public final String fileName() {
            return this.delegate.fileName();
        }
    }
}

